﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;
using RapChieuPhim.DTO;
using System.Data.SqlClient;

namespace RapChieuPhim.DAO
{
    class TheLoaiDAO
    {
        public TheLoaiDTO GetByID(int id)
        {
            DataProvider cn = new DataProvider();
            SqlParameter param = new SqlParameter("@MaTL", id);
            DataTable dt = cn.SqlExcuteQuery("LayTheLoaiTheoID", param);
            TheLoaiDTO TheLoai = new TheLoaiDTO();

            if (dt.Rows.Count != 0)
            {
                TheLoai.MaTL = int.Parse(dt.Rows[0]["MaTL"].ToString());
                TheLoai.TenTL = dt.Rows[0]["TenTL"].ToString();
            }
            return TheLoai;
        }

        public IList GetAll()
        {
            DataProvider cn = new DataProvider();
            ArrayList array = new ArrayList();
            DataTable dt = cn.SqlExcuteQuery("LayAllTheLoai");

            if (dt.Rows.Count != 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    TheLoaiDTO TheLoai = new TheLoaiDTO();
                    TheLoai.MaTL = int.Parse(dr["MaTL"].ToString());
                    TheLoai.TenTL = dr["TenTL"].ToString();
                    array.Add(TheLoai);
                }
            }
            return array;
        }

        public void Insert(TheLoaiDTO TheLoai)
        {
            DataProvider cn = new DataProvider();
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("@TenTL", TheLoai.TenTL);
            cn.SqlExcuteNonQuery("InsertTheLoai", param);
        }

        public void Update(TheLoaiDTO TheLoai)
        {
            DataProvider cn = new DataProvider();
            SqlParameter[] param = new SqlParameter[2];
            param[0] = new SqlParameter("@MaTL", TheLoai.MaTL);
            param[1] = new SqlParameter("@TenTL", TheLoai.TenTL);
            cn.SqlExcuteNonQuery("UpdateTheLoai", param);
        }

        public void Delete(int id)
        {
            DataProvider cn = new DataProvider();
            SqlParameter param = new SqlParameter("@MaTL", id);
            cn.SqlExcuteNonQuery("DeleteTheLoai", param);
        }
    }
}
